package com.heima.wemedia.service.impl;

import com.alibaba.excel.EasyExcel;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.listener.SensitiveEmployee;
import com.heima.wemedia.listener.SensitiveReadListener;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.ExcelService;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class ExcelServiceImpl implements ExcelService {
    private final WmSensitiveService wmSensitiveService;
    private final WmSensitiveMapper wmSensitiveMapper;

    @Override
    public void exportExcel(HttpServletResponse response) {
        // 从数据库查询数据
        List<WmSensitive> wmSensitives = wmSensitiveMapper.selectList(null);

        try {
            // 设置HTTP响应的头信息，告诉浏览器这是一个文件下载
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment; filename=Sensitives.xlsx");

            // 将WmSensitive对象转换为SensitiveEmployee对象
            List<SensitiveEmployee> sensitiveEmployees = wmSensitives.stream()
                    .map(wm -> {
                        SensitiveEmployee se = new SensitiveEmployee();
                        BeanUtils.copyProperties(wm, se);
                        return se;
                    })
                    .collect(Collectors.toList());

            // 使用EasyExcel将数据写入响应流，生成Excel文件
            EasyExcel.write(response.getOutputStream(), SensitiveEmployee.class)
                    .sheet("敏感词")
                    .doWrite(sensitiveEmployees);
        } catch (IOException e) {
            log.error("导出Excel失败", e);
        }
    }

    @Override
    public void importExcel(MultipartFile file) {
        try (InputStream inputStream = file.getInputStream()) {
            // 读取Excel文件并转换为SensitiveEmployee对象列表
            List<SensitiveEmployee> sensitiveEmployees = EasyExcel.read(inputStream, SensitiveEmployee.class,new SensitiveReadListener())
                    .sheet()
                    .doReadSync();

            // 将SensitiveEmployee对象转换为WmSensitive对象并保存到数据库
            List<WmSensitive> wmSensitiveList = sensitiveEmployees.stream()
                    .map(employee -> {
                        WmSensitive wmSensitive = new WmSensitive();
                        wmSensitive.setSensitives(employee.getSensitives());
                        wmSensitive.setCreatedTime(new Date());
                        return wmSensitive;
                    })
                    .collect(Collectors.toList());
            wmSensitiveService.saveBatch(wmSensitiveList);
        } catch (IOException e) {
            log.error("导入Excel失败", e);
        }
    }
}